package com.amazon.shop.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.amazon.shop.bean.dto.BaseGroup;
import com.amazon.shop.bean.dto.BatchExportForm;
import com.amazon.shop.bean.dto.EvaluateDTO;
import com.amazon.shop.bean.entity.RelUserStore;
import com.amazon.shop.bean.enums.ResultEnum;
import com.amazon.shop.bean.vo.PageVO;
import com.amazon.shop.bean.vo.Ret;
import com.amazon.shop.bean.entity.Evalform;
import com.amazon.shop.bean.vo.TblEvalformVO;
import com.amazon.shop.cache.LocalCache;
import com.amazon.shop.service.IEvalformService;
import com.amazon.shop.service.IRelUserStoreService;
import com.amazon.shop.util.ResultUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.sql.Struct;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lzj
 * @since 2021-01-25
 */
@RestController
@RequestMapping("/evalform")
public class EvalformController {

    @Resource
    private IEvalformService esvalformService;

    @Resource
    private IRelUserStoreService relUserStoreService;

    @RequestMapping("/del")
    public Ret del(@Validated(BaseGroup.del.class) EvaluateDTO evaluated){
        esvalformService.removeById(evaluated.getId());
        return ResultUtil.success();
    }

    @RequestMapping("/chengeIpFlag")
    public Ret chengeIpFlag(Integer ipflag){
        return esvalformService.chengeIpFlag(ipflag);
    }

    @RequestMapping("/getChengeIpFlag")
    public Ret getChengeIpFlag(){
        return esvalformService.getChengeIpFlag();
    }


    @RequestMapping("/subEvaluate")
    public Ret subEvaluate(@Validated(BaseGroup.add.class) EvaluateDTO evaluated){
        return esvalformService.subEvaluate(evaluated);
    }



    @RequestMapping("/update")
    public Ret update(@Validated(BaseGroup.update.class) EvaluateDTO evaluateDTO){
        String refundamount = evaluateDTO.getRefundamount();
        String sku = evaluateDTO.getSku();
        String disc = evaluateDTO.getDisc();
        String remark = evaluateDTO.getRemark();
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",evaluateDTO.getId());
        updateWrapper.set("refundamount",StrUtil.isNotBlank(refundamount)?refundamount.trim():refundamount);
        updateWrapper.set("sku",StrUtil.isNotBlank(sku)?sku.trim():sku);
        updateWrapper.set("disc",StrUtil.isNotBlank(disc)?disc.trim():disc);
        updateWrapper.set("remark",StrUtil.isNotBlank(remark)?remark.trim():remark);
        boolean update = esvalformService.update(updateWrapper);
        return update?ResultUtil.success():ResultUtil.error(ResultEnum.FAILURE);
    }


    @RequestMapping("/get")
    public Ret get(EvaluateDTO evaluateDTO){

        Page<Evalform> page = new Page(evaluateDTO.getCurrentNum(),evaluateDTO.getPageSize());
        page.setOrders(OrderItem.descs("createtime"));
        QueryWrapper<Evalform> queryWrapper = new QueryWrapper();
        String operatorUserid = evaluateDTO.getOperatorUserid();

        List<Integer> storeids;
        List<RelUserStore> list;
        if(evaluateDTO.getOismanager()==1){
            list = relUserStoreService.list();
        }else {
            Map parms = new HashMap<>();
            parms.put("userid", Integer.valueOf(operatorUserid));
            list = relUserStoreService.listByMap(parms);
        }
        storeids = list.stream().map(RelUserStore::getStoreid).collect(Collectors.toList());
        if(storeids!=null&&storeids.size()>0){
            queryWrapper.in("storeid",storeids);
            if (evaluateDTO.getStoreid()!=null) {
                queryWrapper.eq("storeid",evaluateDTO.getStoreid());
            }
            if (evaluateDTO.getSatisfied()!=null) {
                queryWrapper.eq("satisfied",evaluateDTO.getSatisfied());
            }
            if(StrUtil.isNotBlank(evaluateDTO.getOrderid())){
                queryWrapper.eq("orderid",evaluateDTO.getOrderid());
            }
            if(evaluateDTO.getIssend()!=null){
                queryWrapper.eq("issend",evaluateDTO.getIssend());
            }
            if(StrUtil.isNotBlank(evaluateDTO.getCountry())){
                queryWrapper.eq("country",evaluateDTO.getCountry());
            }
            if(StrUtil.isNotBlank(evaluateDTO.getRcode())){
                queryWrapper.eq("rcode",evaluateDTO.getRcode());
            }
            if(StrUtil.isNotBlank(evaluateDTO.getSubname())){
                queryWrapper.like("subname",evaluateDTO.getSubname());
            }
            if("null".equals(evaluateDTO.getRefundamount())){
                queryWrapper.isNull("refundamount");
                queryWrapper.or();
                queryWrapper.eq("refundamount","");
            }
            if(StrUtil.isNotBlank(evaluateDTO.getRefundamount())){
                queryWrapper.eq("refundamount",evaluateDTO.getRefundamount());
            }
            if(StrUtil.isNotBlank(evaluateDTO.getSku())){
                queryWrapper.eq("sku",evaluateDTO.getSku());
            }
            if(StrUtil.isNotBlank(evaluateDTO.getDisc())){
                queryWrapper.eq("disc",evaluateDTO.getDisc());
            }
            if(StrUtil.isNotBlank(evaluateDTO.getRemark())){
                queryWrapper.eq("remark",evaluateDTO.getSubname());
            }
            if(StrUtil.isNotBlank(evaluateDTO.getEvalemail())){
                queryWrapper.like("evalemail",evaluateDTO.getEvalemail()+"%");
            }
            if(evaluateDTO.getStime()!=null&&evaluateDTO.getEtime()!=null){
                queryWrapper.ge("createtime",new Date(evaluateDTO.getStime()));
                queryWrapper.lt("createtime",new Date(evaluateDTO.getEtime()));
            }
            page = esvalformService.page(page,queryWrapper);
        }
        List<Evalform> records = page.getRecords();
        List<TblEvalformVO> result = new ArrayList<>();
        for (Evalform record : records) {
            TblEvalformVO tblEvalformVO = new TblEvalformVO();
            BeanUtil.copyProperties(record,tblEvalformVO);
            String storeName = (String) LocalCache.storenameCache.get(record.getStoreid());
            tblEvalformVO.setStorename(storeName);
            result.add(tblEvalformVO);
        }
        PageVO<TblEvalformVO> tblEvalformVOPageVO = new PageVO<>(page);
        tblEvalformVOPageVO.setRecords(result);
        return ResultUtil.success(tblEvalformVOPageVO);
    }

    @RequestMapping("/batchExport")
    public void batchExport(HttpServletResponse response,@Validated BatchExportForm batchExportForm) {
        esvalformService.batchExport(response,batchExportForm);
    }

}
